package t2;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

import t2bean.CardBean;
import t2bean.LibraryBean;

public class CardAction {

	private List<CardBean> list;
	private CardAction bookCardAction;
	private Scanner cin = Cin.open();

	public CardAction() {
	}

	// 办理借书卡
	public LibraryBean add(LibraryBean library) {

		// 输入姓名
		System.out.println("输入姓名：");
		String username = cin.nextLine();

		// 获取已经存储在library中的card数据
		CardBean bookCard = new CardBean();
		list = new ArrayList<CardBean>();
		if (library.getCardList() != null) {
			list = library.getCardList();
		}

		// 将新数据写入list
		if (list != null) {
			bookCard.setId(list.size() + 1);
			bookCard.setName(username);
			list.add(bookCard);
		} else {
			bookCard.setId(1);
			bookCard.setName(username);
			list.add(bookCard);
		}

		// 将library中的数据替换为list
		library.setCardList(list);

		System.out.println("输入0返回,输入1继续办理");
		String str;
		str = cin.nextLine();
		if (str.equals("0")) {
			return library;
		}
		if (str.equals("1")) {
			bookCardAction = new CardAction();
			bookCardAction.add(library);
		}
		return library;
	}

	// 查询某卡借阅记录
	public void searchRecord(LibraryBean library) {

		// 输入数据
		System.out.println("输入查询的姓名：");
		String username = cin.nextLine();

		// 获取存储在library中的数据
		List<CardRecord> list = new ArrayList<CardRecord>();
		if (library.getCardRecordList() != null) {
			list = library.getCardRecordList();
		}

		// 查找输入的姓名匹配的借阅记录
		for (int i = 0; i < list.size(); i++) {
			if (username.equals(list.get(i).getCard().getName())) {
				System.out.println(list.get(i).getInformation());
			}
		}

		System.out.println("输入0返回");
		String str;
		str = cin.nextLine();
		if (str.equals("0")) {
			return;
		}
	}
}
